function [mydemo, cleanup] = demo_rbf_rt_2
%
% Demo of hybrid RBF and regression tree method.
%
% Version 2 (plain forward selection).
%

% Initialise number of chunks in mydemo.
n = 0;

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{
              'This is the demo for the rbf_rt_2 method.', ...
              '-----------------------------------------', ...
              '', ...
              'rbf_rt_2 is an algorithm for regression and classification', ...
              'which combines RBF networks, regression trees and plain', ...
              'forward selection.'}}, ...
  'commands', '', ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'We''re going to compare rbf_rt_2 with its cousin, rbf_rt_1,', ...
              'on one of the two 1D problem.'}}, ...
  'commands', '', ...
  'question', 'Please choose either ''sine1'' or ''hermite''.', ...
  'optional', {{'sine1', 'hermite'}});

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Okay, you chose ''answer''. We''ll next get an instance of', ...
              'a training set for that problem and take a look at it.'}}, ...
  'commands', {{ ...
              '[x, y, dconf] = get_data(''answer'');', ...
              'fig = get_fig(''rbf_rt_2 demo'');', ...
              'hold off', ...
              'plot(x, y, ''r*'')', ...
              '%set(gca, ''XLim'', [dconf.x1 dconf.x2])', ...
              '%set(gca, ''YLim'', [floor(min(y)) ceil(max(y))])', ...
              '%set(gca, ''XTick'', dconf.x1:dconf.x2)', ...
              '%set(gca, ''YTick'', floor(min(y)):ceil(max(y)))', ...
              '%xlabel(''x'', ''FontSize'', 16)', ...
              '%ylabel(''y'', ''FontSize'', 16, ''Rotation'', 0)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'We''ll also get an uncorrupted (zero noise), ordered test set', ...
              'of 400 samples which we can use to judge the accuracy of', ...
              'any methods we apply it to the training set.'}}, ...
  'commands', {{ ...
              'dconf.std = 0;', ...
              'dconf.ord = 1;', ...
              'dconf.p = 400;', ...
              '[xt, yt] = get_data(dconf);', ...
              'hold on', ...
              'plot(xt, yt, ''b-'', ''LineWidth'', 2)'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'Now we''ll run both rbf_rt_1 and rbf_rt_2 on the training set (x,y)', ...
              'to compare them. rbf_rt_1 is usually better. Both methods return', ...
              'the centres (c), radii (r) and weights (w) of an RBF network,', ...
              'together with some auxiliary information in info. We''ll allow both', ...
              'methods to choose default values for all their control parameters.', ...
              '', ...
              'This may take a few seconds.'}}, ...
  'commands', {{ ...
              '[c1, r1, w1, info1] = rbf_rt_1(x, y);', ...
              '[c2, r2, w2, info2] = rbf_rt_2(x, y);'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'So how do the two versions compare? To answer this question', ...
              'we use rbf_dm to get the design matrix of the test set for', ...
              'both networks and multiply these by the weights to get the', ...
              'test set predictions.'}}, ...
  'commands', {{ ...
              'Ht1 = rbf_dm(xt, c1, r1);', ...
              'ft1 = Ht1 * w1;', ...
              'Ht2 = rbf_dm(xt, c2, r2);', ...
              'ft2 = Ht2 * w2;'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', {{ ...
              'These predictions can be plotted along side the data and test set', ...
              'and can also be compared numerically. Did rbf_rt_1 (the yellow', ...
              'curve) get the lowest error again, as it usually does?'}}, ...
  'commands', {{ ...
              'plot(xt, ft1, ''y-'', ''LineWidth'', 2)', ...
              'plot(xt, ft2, ''g-'', ''LineWidth'', 2)', ...
              'disp((yt - ft1)'' * (yt -ft1) / length(yt))', ...
              'disp((yt - ft2)'' * (yt -ft2) / length(yt))'}}, ...
  'question', '', ...
  'optional', '');

n = n + 1;
mydemo(n) = struct( ...
  'comments', 'End of rbf_rt_2 demo.', ...
  'commands', '', ...
  'question', '', ...
  'optional', '');

% Define the commands necessary to cleanup after the demo (e.g. close figures).
cleanup = 'if exist(''fig'', ''var'') if ~isempty(find(findobj(''type'',''figure'') == fig)) close(fig); end; end';
